<?php

namespace App\Logic\finger;

use App\Models\MinerInfo;
use App\Models\User;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;


/**
 * @author AdamTyn
 * @description <用户>数据模型
 */
class MinerLogic
{


    //添加
    public static function add($data){

        $validateRule = [
            'product_id'                          => 'required',
            'capacity'                            => 'required',
            'genuine'                                => 'required',
        ];
        $errorMsg = [
            'product_id.required'                 => '矿机类型不能为空 product_id',
            'capacity.required'                   => '算力不能为空 capacity',
            'genuine.required'                       => '真假值 genuine=genuine,fake',
        ];

        $validator = Validator::make($data, $validateRule, $errorMsg);
        if ($validator->fails()){
            $errors = $validator->errors()->all();
            return e_msg(300001,$errors[0]);
        }

        $product = DB::table('config_products')->where('id',$data['product_id'])->first();
        if(!$product){
            return e_msg(123456,'产品信息不存在');
        }

        $start = strtotime($product->start_time);  //开始时间需要在这个之后

        $time = time();


        //开挖的开始时间
        $start_interval = Cache::get('configs')['mining']['start_interval']->val;
        $start_time = $time + 86400*$start_interval;

        if($start_time < $start){
            $start_time = $start;
        }
        $end_time = $start_time + 86400 * $product->days;

        $arr = [

            'token' => $product->token,
            'hostrate' => $product->hostrate,
            'capacity' => $product->capacity,
            'hspace' => $data['capacity'],
            //'start_time' => $start_time,
            'start_time' => time(),
            'end_time' => $end_time,
            'note' => $data['product_id'],
            'genuine' => $data['genuine'],

        ];

        return MinerInfo::add($arr);

    }

    //绑定用户
    public static function bind($data){

        $validateRule = [
            'mid'                          => 'required',
            'phone'                            => 'required',
            'accessory'                            => 'present',
            'note'                            => 'present',
        ];
        $errorMsg = [
            'mid.required'                 => '矿机mid不能为空',
            'phone.required'                   => '用户手机号phone不能为空',
            'note.present'                   => '备注字段要存在 note',
            'accessory.present'                   => '附件字段要存在 accessory',
        ];

        $validator = Validator::make($data, $validateRule, $errorMsg);
        if ($validator->fails()){
            $errors = $validator->errors()->all();
            return e_msg(300001,$errors[0]);
        }

        $username = User::where('phone',$data['phone'])->value('username');
        if(!$username){
            return e_msg(123456,'用户信息不存在');
        }
        $m_number = MinerInfo::where('mid',$data['mid'])->value('m_number');
        if(!$m_number){
            return e_msg(123456,'矿机信息不存在');
        }

        $already = DB::table('miner_user')->where('mid',$data['mid'])->value('user_name');
        if($already){
            return e_msg(123456,'矿机已绑定过用户'.$already);
        }

        $arr = [
            'user_name'                 => $username,
            'mid'                 => $data['mid'],
            'note'                 => $data['note'],
            'accessory'                 => $data['accessory'],
        ];

        return MinerInfo::bind($arr);

    }

	
	//修改备注和附件
    public static function edit($data){

        $validateRule = [
            'mid'                          => 'required',
            'accessory'                            => 'present',
            'note'                            => 'present',
        ];
        $errorMsg = [
            'mid.required'                 => '矿机mid不能为空',
            'note.present'                   => '备注字段要存在 note',
            'accessory.present'                   => '附件字段要存在 accessory',
        ];

        $validator = Validator::make($data, $validateRule, $errorMsg);
        if ($validator->fails()){
            $errors = $validator->errors()->all();
            return e_msg(300001,$errors[0]);
        }

        $m_number = MinerInfo::where('mid',$data['mid'])->value('m_number');
        if(!$m_number){
            return e_msg(123456,'矿机信息不存在');
        }

        $already = DB::table('miner_user')->where('mid',$data['mid'])->value('id');
        if(!$already){
            return e_msg(123456,'矿机未绑定过用户'.$already);
        }


        $arr = [
            'mid'                 => $data['mid'],
            'note'                 => $data['note'],
            'accessory'                 => $data['accessory'],
        ];
        $rel = DB::table('miner_user')->where('id',$already)->update($arr);
        return s_msg('');

    }
}
